/** 
 * 
 * @author geloin 
 * @date 2012-5-5 涓婂崍9:31:52 
 */
package com.wcs.spring.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wcs.spring.entity.User;
import com.wcs.spring.service.UserService;
import com.wcs.spring.util.Const;

@Controller
@RequestMapping(value = "mgt/sys")
public class LoginController {
	@Resource(name = "userService")
	private UserService userService;

	@RequestMapping(value = "toLogin")
	public ModelAndView toLogin(HttpServletResponse response) throws Exception {
		return new ModelAndView("mgt/login");
	}

	@RequestMapping(value = "login")
	public @ResponseBody
	Map<String, Object> login(User user, Integer remember, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		HttpSession session = request.getSession();
		Integer schoolId = user.getSchoolId();
		Integer flag = 1;
		String message = "";
		user.setSchoolId(null);
		User user2 = userService.queryUserByParam(user);
		if (user2 != null) {
			if ("zj".equals(user2.getUserType())) {
				user2.setSchoolId(user.getSchoolId());
				flag = 0;
				session.setAttribute(Const.SESSION_SYS_USER, user2);
			}
			else {
				if (user2.getSchoolId() != schoolId) {
					message = "您选择的校区不存在此用户！";
				}
				else {
					flag = 0;
					session.setAttribute(Const.SESSION_SYS_USER, user2);
				}
			}
		}
		else {
			message = "用户名或密码有误！";
		}
		if (flag == 0) {
			if (null != remember && remember == 1) {
				Cookie cookie = new Cookie("sysUserName", user2.getUserName());
				cookie.setMaxAge(30 * 24 * 60 * 60);
				response.addCookie(cookie);
			}
			else {
				Cookie cookie = new Cookie("sysUserName", null);
				cookie.setMaxAge(0);
				response.addCookie(cookie);
			}

		}
		map.put("flag", flag);
		map.put("message", message);
		return map;
	}

	@RequestMapping(value = "logout")
	public ModelAndView logout(User user, HttpServletRequest request) throws Exception {
		request.getSession().removeAttribute(Const.SESSION_SYS_USER);
		return new ModelAndView("redirect:/mgt/sys/toLogin.do");
	}

	@RequestMapping(value = "index")
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		User sessionUser = (User) request.getSession().getAttribute(Const.SESSION_SYS_USER);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("schoolId", sessionUser.getSchoolId());
		map.put("schoolIdName", sessionUser.getSchoolIdName());
		return new ModelAndView("mgt/index", map);
	}

	@RequestMapping(value = "content")
	public ModelAndView content(Integer schoolId, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		Map<String, Object> map = new HashMap<String, Object>();
		User user = (User) session.getAttribute(Const.SESSION_SYS_USER);
		if ("zj".equals(user.getUserType()) && null != schoolId) {
			user.setSchoolId(schoolId);
			session.setAttribute(Const.SESSION_SYS_USER, user);
		}
		return new ModelAndView("mgt/content", map);
	}

	@RequestMapping(value = "blank")
	public ModelAndView blank(HttpServletResponse response) throws Exception {
		return new ModelAndView("mgt/blank");
	}

	@RequestMapping(value = "calendar")
	public ModelAndView calendar(HttpServletResponse response) throws Exception {
		return new ModelAndView("mgt/calendar");
	}

	@RequestMapping(value = "chart")
	public ModelAndView chart(HttpServletResponse response) throws Exception {
		return new ModelAndView("mgt/chart");
	}

	@RequestMapping(value = "form")
	public ModelAndView form(HttpServletResponse response) throws Exception {
		return new ModelAndView("mgt/form");
	}

	@RequestMapping(value = "gallery")
	public ModelAndView gallery(HttpServletResponse response) throws Exception {
		return new ModelAndView("mgt/gallery");
	}

	@RequestMapping(value = "grid")
	public ModelAndView grid(HttpServletResponse response) throws Exception {
		return new ModelAndView("mgt/grid");
	}

	@RequestMapping(value = "icon")
	public ModelAndView icon(HttpServletResponse response) throws Exception {
		return new ModelAndView("mgt/icon");
	}

	@RequestMapping(value = "table")
	public ModelAndView table(HttpServletResponse response) throws Exception {
		return new ModelAndView("mgt/table");
	}

	@RequestMapping(value = "tour")
	public ModelAndView tour(HttpServletResponse response) throws Exception {
		return new ModelAndView("mgt/tour");
	}

	@RequestMapping(value = "typography")
	public ModelAndView typography(HttpServletResponse response) throws Exception {
		return new ModelAndView("mgt/typography");
	}

	@RequestMapping(value = "ui")
	public ModelAndView ui(HttpServletResponse response) throws Exception {
		return new ModelAndView("mgt/ui");
	}

	@RequestMapping(value = "error")
	public ModelAndView error(HttpServletResponse response) throws Exception {
		return new ModelAndView("mgt/error");
	}

}
